<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="keyword" content="ONE,GO物,网上购物,交易市场,网上交易">
    <meta name="description" content="ONE-GO物，平价线上交易平台，提供各类商品交易，让大家都可以享受购物的乐趣和真正品质购物">
    <title>ONE - GO物 | 登陆</title>
    <link rel="shortcut icon" href="/static/images/logo_i.png">
    <!--引入layui css样式-->
    <link rel="stylesheet" href="/static/layui-v2.6.7/layui/css/layui.css">
    <!--引入layui js脚本-->
    <script src="/static/layui-v2.6.7/layui/layui.js"></script>
    <link rel="stylesheet" href="/static/css/register.css">
</head>
<body class="layui-bg-red">
<!--主体 start-->
<div class="layui-container content layui-anim layui-anim-scale">
    <div class="logo-box">
        <a href="/static/index.html">
            <img src="/static/images/logo_w.png" alt="ONE-GO物" class="logo">
        </a>
        <h2>ONE-GO物</h2>
        <p>2021 - 现在 yige@yige.com 版权所有</p>
    </div>
    <div class="form-box">
        <h3 class="title">欢迎登陆</h3>
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱/账号</label>
                <div class="layui-input-block">
                    <input type="text" name="email" required lay-verify="required|email" placeholder="请输入邮箱/账号"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" required lay-verify="required|pass" placeholder="请输入密码"
                           autocomplete="off"
                           class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="re_password" required lay-verify="required|pass|re_pass"
                           placeholder="请确认密码"
                           autocomplete="off"
                           class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                    <input type="verify" name="verify" required lay-verify="required|verify"
                           placeholder="请输入验证码"
                           autocomplete="off"
                           class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="padding: 0px !important" id="verification-image-box">

                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="login">立即登陆</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <div class="go-login"><a href="/static/page/user/register.html">暂无账号 ，前往注册</a></div>
    </div>
</div>
<!--主体 end-->

<script type="text/javascript">
    layui.use(['form', 'jquery', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.$;

        //自定义验证规则
        form.verify({
            pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ],
            re_pass: function (value, item) {
                console.log(value)
                if (value != $('input[name="password"]').val()) {
                    return '两次密码不一致'
                }
            },
            verify: [
                /^[\S]{4}$/
                , '验证码必须是4位'
            ],
        });


        function generateUUID() {
            let d = new Date().getTime()
            let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                let r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d / 16);
                return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16)
            })
            return uuid;
        }

        let uuid = generateUUID()

        $('#verification-image-box').html('<img alt="请稍后重试" id="verification-image">')

        function get_verification_image() {
            $("#verification-image").attr("src", "/user/image/verification/" + uuid + "?date=" + new Date().getTime());
        }

        get_verification_image()

        $('#verification-image-box').click(get_verification_image)

        setInterval(get_verification_image, 1000 * 60 * 3)


        //监听提交
        form.on('submit(login)', function (data) {
            $.post("/user/login/" + uuid, data.field, function (data) {
                let code = data.code
                if (code == 200) {
                    layer.msg('登陆成功', {icon: 6})
                    localStorage.setItem('Authorization', data.data)
                    setInterval(function () {
                        location.href = '/static/index.html'
                    }, 1000)
                } else if (code == 4004 || code == 4001) {
                    layer.msg('验证码错误', {icon: 5})
                    get_verification_image()
                } else if (code == 4002) {
                    layer.msg('账号或密码错误', {icon: 5})
                    get_verification_image()
                } else {
                    layer.msg(data.msg, {icon: 5})
                    return false
                }
                return false
            })
            return false
        });
    })
</script>
</body>
</html>